package com.wc.alorithm_blue_bridge_history.fourteen.与或异或;

/**
 * @Author congge
 * @Date 2024/1/15 19:49
 * @description 与或异或
 * https://www.lanqiao.cn/problems/3552/learning/?page=1&first_category_id=1
 */
public class Main {
    static char[] ops = new char[10];
    static int ans = 0;
    static char[] op = new char[]{'&', '^', '|'};
    static int[][] grip = new int[5][5];

    public static void main(String[] args) {
        grip[0][0] = 1;
        grip[0][1] = 0;
        grip[0][2] = 1;
        grip[0][3] = 0;
        grip[0][4] = 1;
        dfs(0);
        System.out.println(ans);
    }

    public static void dfs(int floor) {
        if (floor == 10) {
            if (check()) {
                ans++;
            }
            return;
        }
        for (int i = 0; i < 3; i++) {
            ops[floor] = op[i];
            dfs(floor + 1);
        }
    }

    static boolean check() {
        int idx = 0;
        for (int i = 1; i < 5; i++) {
            for (int j = 0; j < 5 - i; j++) {
                grip[i][j] = op(grip[i - 1][j], grip[i - 1][j + 1], ops[idx++]);
            }
        }
        return grip[4][0] == 1;
    }

    static int op(int left, int right, char op) {
        switch (op) {
            case '|':
                return left | right;
            case '&':
                return left & right;
            case '^':
                return left ^ right;
        }
        return -1;
    }
}
